package com.d3works.apps.businessproduction.service;

import com.d3works.apps.account.db.AnalyticMoveLine;
import com.d3works.apps.account.db.repo.AnalyticMoveLineRepository;
import com.d3works.apps.base.db.CancelReason;
import com.d3works.apps.base.db.repo.PartnerRepository;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.base.service.user.UserService;
import com.d3works.apps.production.service.SaleOrderWorkflowServiceProductionImpl;
import com.d3works.apps.production.service.app.AppProductionService;
import com.d3works.apps.production.service.productionorder.ProductionOrderSaleOrderService;
import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.apps.sale.db.repo.SaleOrderRepository;
import com.d3works.apps.sale.service.app.AppSaleService;
import com.d3works.apps.sale.service.config.SaleConfigService;
import com.d3works.apps.sale.service.saleorder.SaleOrderLineService;
import com.d3works.apps.supplychain.service.AccountingSituationSupplychainService;
import com.d3works.apps.supplychain.service.PartnerSupplychainService;
import com.d3works.apps.supplychain.service.SaleOrderCheckAnalyticService;
import com.d3works.apps.supplychain.service.SaleOrderPurchaseService;
import com.d3works.apps.supplychain.service.SaleOrderStockService;
import com.d3works.apps.supplychain.service.app.AppSupplychainService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class SaleOrderWorkflowServiceBusinessProductionImpl
    extends SaleOrderWorkflowServiceProductionImpl {

  private AnalyticMoveLineRepository analyticMoveLineRepository;

  @Inject
  public SaleOrderWorkflowServiceBusinessProductionImpl(
      SequenceService sequenceService,
      PartnerRepository partnerRepo,
      SaleOrderRepository saleOrderRepo,
      AppSaleService appSaleService,
      UserService userService,
      SaleOrderLineService saleOrderLineService,
      SaleOrderStockService saleOrderStockService,
      SaleOrderPurchaseService saleOrderPurchaseService,
      AppSupplychainService appSupplychainService,
      AccountingSituationSupplychainService accountingSituationSupplychainService,
      ProductionOrderSaleOrderService productionOrderSaleOrderService,
      AppProductionService appProductionService,
      AnalyticMoveLineRepository analyticMoveLineRepository,
      PartnerSupplychainService partnerSupplychainService,
      SaleConfigService saleConfigService,
      SaleOrderCheckAnalyticService saleOrderCheckAnalyticService) {
    super(
        sequenceService,
        partnerRepo,
        saleOrderRepo,
        appSaleService,
        userService,
        saleOrderLineService,
        saleOrderStockService,
        saleOrderPurchaseService,
        appSupplychainService,
        accountingSituationSupplychainService,
        productionOrderSaleOrderService,
        appProductionService,
        partnerSupplychainService,
        saleConfigService,
        saleOrderCheckAnalyticService);
    this.analyticMoveLineRepository = analyticMoveLineRepository;
  }

  @Override
  @Transactional(rollbackOn = Exception.class)
  public void cancelSaleOrder(
      SaleOrder saleOrder, CancelReason cancelReason, String cancelReasonStr)
      throws D3worksException {
    super.cancelSaleOrder(saleOrder, cancelReason, cancelReasonStr);
    for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
      for (AnalyticMoveLine analyticMoveLine : saleOrderLine.getAnalyticMoveLineList()) {
        analyticMoveLine.setProject(null);
        analyticMoveLineRepository.save(analyticMoveLine);
      }
    }
  }
}
